﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using sonicERPinfoCsharp;

namespace sonicERPDALCsharp
{
    
    
  
    public  abstract  class DAL
    {
        #region variaveis globais
        conexao con = new conexao();
       
        #endregion
      
       
        #region propriedades

        private static string select_;

        public static string Select_
        {
            get { return DAL.select_; }
            set { DAL.select_ = value; }
        }


        private static bool tipocomando;

        public static bool Tipocomando
        {
            get { return DAL.tipocomando; }
            set { DAL.tipocomando = value; }
        }
      

        private static ArrayList entidade ;

        public static ArrayList Entidade
        {
            get { return DAL.entidade; }
            set { DAL.entidade = value; }
        }


        private static ArrayList parameter ;

        public static ArrayList Parameter
        {
            get { return DAL.parameter; }
            set { DAL.parameter = value; }
        }


        private static bool existepametro;

        public static bool Existepametro
        {
            get { return DAL.existepametro; }
            set { DAL.existepametro = value; }
        }

        #endregion

        #region procedimentos

        private   void limparpropriedades()
        {
            select_ = null;
            Entidade = null;
            Parameter = null;
            tipocomando = false;
            existepametro = false;
       
        }

        

        

      
       //--------------------------------------------------------
        private void sqlExecuteNonQuery()
        {
            try
            {
                string cone = con.conexaostring();
            
                SqlConnection cn = new SqlConnection(cone);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = select_;
                if (tipocomando )
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                }
                else
                {
                    cmd.CommandType = CommandType.Text;
                }
                if (existepametro)
                {
                    for (int i = 0; i < parameter.Count; i++)
                    {
                        cmd.Parameters.AddWithValue(parameter[i].ToString(), entidade[i].ToString());
                    }
                }
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
                limparpropriedades();

            }
            catch (SqlException ex)
            {
                
                throw new Exception(ex.Message);
            }
        }

        #endregion
        // ---------------------------------------------------------

        #region funções
        

        private DataView SqlDataAdapter()
        {
            try
            {
                string cn = con.conexaostring();

                SqlDataAdapter cmd = new SqlDataAdapter(Select_, cn );
              
                if (Tipocomando)
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                 else
                cmd.SelectCommand.CommandType = CommandType.Text;
                
                if (Existepametro)
                {
                   
                    for (int i = 0; i < Parameter.Count ; i++)
                    {
                        cmd.SelectCommand.Parameters.AddWithValue(Parameter[i].ToString(), Entidade[i].ToString());
                    }
                }
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                DataView dv = new DataView(dt);
                limparpropriedades();
                return dv;
            }
            catch (SqlException ex)
            {

                throw new Exception(ex.Message);
            }

        }
        //----------------------------------------------------------
        #endregion

        #region eventos 
       
       


        #endregion

        #region metodos sobreescritos
        public virtual DataView getdados(info tabela)
        {
            try
            {

            return SqlDataAdapter();
            
            }
            catch (SqlException ex )
            {

                throw new Exception(ex.Message);
            }
            
        }
        //------------------------------------------------------
        public virtual void insert(info tabela)
        {
            try
            {
                sqlExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                
                throw new Exception(ex.Message);
            }
            
        }
        //-----------------------------------------------
        public virtual void delete(info tabela)
        {
            try
            {
                sqlExecuteNonQuery();
            }
            catch (SqlException ex)
            {

                throw new Exception(ex.Message);
            }

        }
        //---------------------------------------------
        public virtual void update(info tabela)
        {
            try
            {
                sqlExecuteNonQuery();
            }
            catch (SqlException ex)
            {

                throw new Exception(ex.Message);
            }

        }

        #endregion
    }
}
